package com.big.order.controller.api;

import com.big.common.result.Result;
import com.big.common.result.ResultCodeEnum;
import com.big.common.utils.Conversion;
import com.big.model.model.order.MerchandiseOrder;
import com.big.model.model.record.MerchandiseOrderRecord;
import com.big.model.vo.merchandise.MerchanCommVo;
import com.big.model.vo.merchandise.MerchandiseOrderVo;
import com.big.model.vo.order.OrderVo;
import com.big.order.service.OrderRecordService;
import com.big.order.service.OrderService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("/api/order")
public class OrderRecordController {



    @Autowired
    OrderRecordService merchandiseOrderRecordService;

    @Autowired
    OrderService orderService;


    @ApiModelProperty("订单记录添加")
    @PostMapping("/auth/save")
     public Result save(Map<String,Object>bat){
        Object dId = bat.get("DID");
        Object obj = bat.get("merchandiseOrderVos");
        List<MerchandiseOrderVo> list = Conversion.castList(obj, MerchandiseOrderVo.class);
        List<MerchandiseOrderRecord> merchandiseOrderRecords= new ArrayList<>();
        list.forEach(item -> {
            MerchandiseOrderRecord merchandise=new MerchandiseOrderRecord();
            merchandise.setMId(Long.valueOf(item.getMid()));
            merchandise.setMNum(Integer.valueOf(item.getNum()));
            merchandise.setOId(String.valueOf(dId));//接入订单号
            merchandise.setMPrice(item.getPrice());
            merchandise.setMSubtotal(Double.valueOf(item.getPrice())*Integer.valueOf(item.getNum()));
            merchandise.setPackId(Long.valueOf(item.getSpackage()));
            merchandiseOrderRecords.add(merchandise);
        });
         merchandiseOrderRecordService.saveMerchandiseOrderRecord(merchandiseOrderRecords);
         return Result.ok();
    }

    @ApiModelProperty("获取集合")
    @PostMapping("/auth/obtains")
    public Result obtains(@RequestBody List<MerchandiseOrderVo> merchandiseOrderVos, OrderVo orderVo) throws ExecutionException {
        Map<String,Object> bat = new HashMap<>();
        merchandiseOrderVos.forEach(item -> {
            System.out.println(item);
        });
        //获取商品集合
        bat.put("merchandiseOrderVos",merchandiseOrderVos);
        //生成订单
        bat.put("DID",this.saveOrder(orderVo));
        //TODO 进行操作 写入数据库&&发送短信
        this.save(bat);
        return Result.ok();
    }



    //创建订单
    @ApiModelProperty(value = "创建订单")
    @PostMapping("/auth/saveOrder")
    public String saveOrder(OrderVo orderVo) throws ExecutionException {
        String DID = orderService.saveMerchandiseOrder(null, orderVo);
        return  DID;
    }


    //订单列表
    @ApiModelProperty(value = "订单列表")
    @PostMapping("/auth/infoOrders/{uid}")
    public Result infoOrders(@PathVariable("uid") String uid) throws ExecutionException {
        List<MerchandiseOrder> merchandiseOrders = orderService.infoMerchandiseOrders(uid);
        return Result.ok(merchandiseOrders);
    }


    //查询订单
    @ApiModelProperty(value = "查询订单")
    @PostMapping("/auth/findOrder/{oid}")
    public Result findOrder(@PathVariable("oid") String oid) {
        MerchandiseOrder merchandiseOrder = orderService.infoMerchandiseOrder(oid);
        return Result.ok(merchandiseOrder);
    }



    //修改订单
    @ApiModelProperty(value = "修改订单状态")
    @PostMapping("/auth/updateOrder")
    public Integer  UpdateOrder(@RequestParam("oId") String orderId,
                              @RequestBody ResultCodeEnum resultCodeEnum) {
        return orderService.upOrderStatus(orderId, resultCodeEnum);
    }

    /**
     *  订单商品描述
     */
    @ApiModelProperty(value = "订单商品描述")
    @GetMapping("/auth/OrderRecords/{oId}")
    public Result  MerchandiseOrderRecords(@PathVariable("oId") String oId) {
        List<MerchandiseOrderRecord> merchandiseOrderRecordList = merchandiseOrderRecordService.infoMerchandiseOrderRecords(oId);
        return Result.ok(merchandiseOrderRecordList);
    }


    /**
     *  订单签收
     */
    @ApiModelProperty(value = "订单签收")
    @PostMapping("/auth/signOrder")
    public Result  signOrder(@RequestBody  MerchanCommVo merchanCommVo) {
        System.out.println(merchanCommVo);
        return Result.ok(orderService.signOrder(merchanCommVo));
    }



}
